<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入服务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="访问路径" prop="predicate">
        <el-input
          v-model="queryParams.predicate"
          placeholder="请输入访问路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="状态" prop="disabled" >
        <el-select v-model="queryParams.disabled" placeholder="请选择状态">
          <el-option label="启用" value="0"></el-option>
          <el-option label="禁用" value="1"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="是否删除 0：未删除 1：已删除" prop="deleted">
        <el-input
          v-model="queryParams.deleted"
          placeholder="请输入是否删除 0：未删除 1：已删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="创建时间" prop="created">
        <el-date-picker clearable
          v-model="queryParams.created"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>-->
<!--      <el-form-item label="创建人" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="更新时间" prop="updated">
        <el-date-picker clearable
          v-model="queryParams.updated"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>-->
<!--      <el-form-item label="更新人" prop="updatedBy">
        <el-input
          v-model="queryParams.updatedBy"
          placeholder="请输入更新人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gatewayadmin:router:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gatewayadmin:router:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gatewayadmin:router:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gatewayadmin:router:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="routerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="60">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="服务名称" align="center" prop="name" />
      <el-table-column label="服务描述" align="center" prop="descript" />
      <el-table-column label="访问路径" align="center" prop="predicate" />
      <el-table-column label="转发路径" align="center" prop="uri" />
      <el-table-column label="文档地址" align="center" prop="docUrl" />
      <el-table-column
        label="状态"
        align="center"
        prop="disabled"
      >
        <template v-slot="scope">
          <span>{{ scope.row.disabled == 1 ? '禁用' : '启用' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="created" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.created, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="更新时间" align="center" prop="updated" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updated, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updatedBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gatewayadmin:router:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gatewayadmin:router:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改路由定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入服务名称" />
        </el-form-item>
        <el-form-item label="服务描述" prop="descript">
          <el-input v-model="form.descript" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="访问路径" prop="predicate">
          <el-input v-model="form.predicate" placeholder="请输入访问路径" />
        </el-form-item>
        <el-form-item label="转发路径" prop="uri">
          <el-input v-model="form.uri" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文档地址" prop="docUrl">
          <el-input v-model="form.docUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态" prop="disabled">
          <el-button 
            :type="form.disabled === '1' ? 'danger' : 'success'"
            size="small"
            @click="toggleDisabled">
            {{ form.disabled === '1' ? '禁用' : '启用' }}
          </el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRouter, getRouter, delRouter, addRouter, updateRouter } from "@/api/gatewayadmin/router";

export default {
  name: "Router",
  data() {
    // 自定义 URL 验证规则
    const validateUrl = (rule, value, callback) => {
      if (value && !/^(http|https):\/\/[^\s/$.?#].[^\s]*$/.test(value)) {
        callback(new Error('请输入有效的 URL 地址'));
      } else {
        callback();
      }
    };

    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 路由定义表格数据
      routerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        descript: null,
        predicate: null,
        uri: null,
        docUrl: null,
        disabled: null,
        deleted: null,
        created: null,
        createdBy: null,
        updated: null,
        updatedBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "服务名称不能为空", trigger: "blur" }
        ],
        predicate: [
          { required: true, message: "访问路径不能为空", trigger: "blur" }
        ],
        uri: [
          { required: true, message: "转发路径不能为空", trigger: "blur" },
          { validator: validateUrl, trigger: 'blur' }
        ],
        disabled: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询路由定义列表 */
    getList() {
      this.loading = true;
      listRouter(this.queryParams).then(response => {
        this.routerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uuid: null,
        name: null,
        descript: null,
        predicate: null,
        uri: null,
        docUrl: null,
        disabled: '0', // 默认设置为启用
        deleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uuid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加路由定义";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const uuid = row.uuid || this.ids
      getRouter(uuid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改路由定义";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uuid != null) {
            updateRouter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRouter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮作 */
    handleDelete(row) {
      const uuids = row.uuid || this.ids;
      const names = row.name || this.routerList.filter(item => this.ids.includes(item.uuid)).map(item => item.name).join(', ');
      
      this.$modal.confirm(`确认删除"${names}"服务？`).then(() => {
        return delRouter(uuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gatewayadmin/router/export', {
        ...this.queryParams
      }, `router_${new Date().getTime()}.xlsx`)
    },
    toggleDisabled() {
      this.form.disabled = this.form.disabled === '1' ? '0' : '1';
    }
  }
};
</script>

<style scoped>
.el-button--small {
  padding: 9px 15px;
  font-size: 12px;
  border-radius: 3px;
}
.custom-status-button {
  padding: 8px 20px;
  font-size: 14px;
  border-radius: 20px;
  border: none;
  color: white;
  transition: all 0.3s ease;
}
.custom-status-button.enabled {
  background-color: #67C23A;
}
.custom-status-button.disabled {
  background-color: #909399;
}
.custom-status-button:hover {
  opacity: 0.8;
}
.status-tag {
  cursor: pointer;
  padding: 8px 12px;
  font-size: 14px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.status-tag:hover {
  opacity: 0.8;
}

/* 可选：添加一些动画效果 */
.status-tag {
  position: relative;
  overflow: hidden;
}

.status-tag::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 5px;
  height: 5px;
  background: rgba(255, 255, 255, 0.5);
  opacity: 0;
  border-radius: 100%;
  transform: scale(1, 1) translate(-50%);
  transform-origin: 50% 50%;
}

.status-tag:hover::after {
  animation: ripple 1s ease-out;
}

@keyframes ripple {
  0% {
    transform: scale(0, 0);
    opacity: 0.5;
  }
  20% {
    transform: scale(25, 25);
    opacity: 0.5;
  }
  100% {
    opacity: 0;
    transform: scale(40, 40);
  }
}
</style>



